/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the lkyh.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.lkyh.mes.codegen.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkyh.mes.codegen.entity.GenFormDo;
import com.lkyh.mes.codegen.entity.GenFormDoColumn;
import com.lkyh.mes.codegen.entity.GenTableInfo;
import com.lkyh.mes.codegen.mapper.GenFormDoMapper;
import com.lkyh.mes.codegen.service.GenFormDoColumnService;
import com.lkyh.mes.codegen.service.GenFormDoService;
import com.lkyh.mes.codegen.service.GenTableInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 表单管理
 *
 * @author lengleng
 * @date 2019-08-12 15:55:35
 */
@Service
//@AllArgsConstructor
public class GenFormDoServiceImpl extends ServiceImpl<GenFormDoMapper, GenFormDo> implements GenFormDoService {
	@Resource
	private GenFormDoColumnService genFormDoColumnService;
	@Resource
	private GenTableInfoService genTableInfoService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean save(GenFormDo genFormDo) {
		super.save(genFormDo);
		GenTableInfo genTableInfo = genTableInfoService.getByIdWithChild(genFormDo.getDoTableId());

		List<GenFormDoColumn> genFormDoColumnList = genTableInfo.getGenColumnInfoList().stream().map(
				i -> new GenFormDoColumn()
						.setDoId(genFormDo.getId())
						.setDoTableId(genFormDo.getDoTableId())
						.setColumnId(i.getId()))
				.collect(Collectors.toList());
		genFormDoColumnService.saveBatch(genFormDoColumnList);
		return false;
	}

	@Override
	public List<GenFormDo> listWithChild(GenFormDo entity) {
		List<GenFormDo> list = this.list(Wrappers.query(entity));
		if (CollUtil.isNotEmpty(list)) {
			list.forEach(i -> i.setGenFormDoColumnList(this.genFormDoColumnService.list(Wrappers.query(new GenFormDoColumn().setDoId(i.getId())))));
		}
		return list;
	}

	@Override
	public boolean remove(Wrapper<GenFormDo> wrapper) {
		if (super.remove(wrapper)) {
			return genFormDoColumnService.remove(Wrappers.query(new GenFormDoColumn().setDoId(wrapper.getEntity().getId())));
		}
		return false;
	}
}
